//
//  DXController.m
//  suremodv
//
//  Created by dd on 13-9-17.
//  Copyright (c) 2013年 xiaomuzhi. All rights reserved.
//

#import "DXController.h"

@interface DXController ()

@end

@implementation DXController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadView
{
    [super loadView];
    
    _ad = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7"))
    {
        self.automaticallyAdjustsScrollViewInsets = NO;
        _screenHeight = [DX screenHeight];
    }
    else
    {
        _screenHeight = [DX screenHeight] - 20;
    }
    
    _screenAvailableHeight = [DX screenHeight] - 20 - 44;
    
    _data = [DXData sharedInstance];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 21)];
    [backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backEventHandler) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    
    UIImageView *bg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, _screenAvailableHeight)];
    bg.image = [UIImage imageNamed:@"bg"];
    [self.view addSubview:bg];
    
    UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgHandler)];
    tgr.numberOfTapsRequired = 1;
    bg.userInteractionEnabled = YES;
    tgr.delegate = self;
    [bg addGestureRecognizer:tgr];
    
    _holder = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, _screenAvailableHeight)];
    _holder.userInteractionEnabled = YES;
    _holder.showsHorizontalScrollIndicator = NO;
    _holder.showsVerticalScrollIndicator = NO;
    _holder.contentSize = CGSizeMake(320, _screenAvailableHeight);
    [self.view addSubview:_holder];
    
    UITapGestureRecognizer *tgr1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bgHandler)];
    tgr1.numberOfTapsRequired = 1;
    tgr1.delegate = self;
    [_holder addGestureRecognizer:tgr1];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShowEventHandler:) name:UIKeyboardDidShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHideEventHandler:) name:UIKeyboardDidHideNotification object:nil];
    
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return !([touch.view isKindOfClass:[UIControl class]]);
}

- (void)backEventHandler
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)bgHandler
{
    [self.view endEditing:YES];
}





- (void)keyboardDidShowEventHandler:(NSNotification *)notification
{
    NSDictionary* info = [notification userInfo];
    CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
    _kbHeight = kbSize.height;
    
    [self updateScroll];
    
}

/*
 - (void)keyboardDidChangeEventHandler:(NSNotification *)notification
 {
 NSDictionary* info = [notification userInfo];
 CGSize kbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
 
 UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbSize.height, 0.0);
 _holder.contentInset = contentInsets;
 _holder.scrollIndicatorInsets = contentInsets;
 
 
 if (_scrollFlagY > _holder.frame.size.height - kbSize.height )
 {
 CGPoint scrollPoint = CGPointMake(0.0, _scrollFlagY - _holder.frame.size.height + kbSize.height);
 [_holder setContentOffset:scrollPoint animated:NO];
 }
 
 }
 */

- (void)keyboardDidHideEventHandler:(NSNotification *)notification
{
    _kbHeight = 0;
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    _holder.contentInset = contentInsets;
    _holder.scrollIndicatorInsets = contentInsets;
    [_holder setContentOffset:CGPointMake(0, 0) animated:YES];
    
}

- (void)holderFit
{
    _holder.contentSize = CGSizeMake(320, _holder.frame.size.height);
}

- (void)updateScroll
{
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, _kbHeight, 0.0);
    _holder.contentInset = contentInsets;
    _holder.scrollIndicatorInsets = contentInsets;
    
    // DXLog(@" %f %f", _scrollFlagY, _lastScrollY);
    
    if (_scrollFlagY > _holder.frame.size.height - _kbHeight )
    {
        CGPoint scrollPoint = CGPointMake(0.0, _scrollFlagY - _holder.frame.size.height + _kbHeight);
        
        // DXLog(@"::::: %f %f", scrollPoint.y, _lastScrollY);
        
        if (scrollPoint.y > _lastScrollY)
        {
            [UIView animateWithDuration:0.2
                                  delay:0
                                options:UIViewAnimationOptionAllowAnimatedContent
                             animations:^{
                                 _holder.frame = CGRectMake(0, -1 * (scrollPoint.y - _lastScrollY), 320, _holder.frame.size.height);
                             }
                             completion:^(BOOL finished) {
                                 
                                 [_holder setContentOffset:scrollPoint animated:NO];
                                 _holder.frame = CGRectMake(0, 0, 320, _holder.frame.size.height);
                                 
                             }];
        }
        else
        {
            [_holder setContentOffset:scrollPoint animated:YES];
        }
        _lastScrollY = scrollPoint.y;
    }
}

@end
